﻿Imports System.Windows.Forms

Public Class Form2

    Dim count As Integer = 0
    Dim pie As HtmlElementCollection

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dialog1.Close()
        Form1.Close()
        WebBrowser1.Navigate("https://ic.dist113.org/campus/portal/main.xsl")
    End Sub

    Private Sub WebBrowser1_ProgressChanged() Handles WebBrowser1.DocumentCompleted
        count = count + 1
        If count = 3 Then
loading1:   Try
                WebBrowser1.Document.Window.Frames.Item(0).Document.GetElementById("schedule").InvokeMember("click")
            Catch ex As NullReferenceException
                For x As Integer = 1 To 10
                    System.Threading.Thread.Sleep(100)
                    Application.DoEvents()
                Next
                GoTo loading1
            End Try
        End If
        If count = 4 Then
            getthepages()
        End If
    End Sub

    Private Sub getthepages()
        For x As Integer = 1 To 15
            System.Threading.Thread.Sleep(100)
            Application.DoEvents()
        Next
        pie = WebBrowser1.Document.Window.Frames.Item(0).Document.GetElementsByTagName("a")
        For counter As Integer = 0 To pie.Count - 1
loading2:   Try
                If WebBrowser1.Document.Window.Frames.Item(0).Document.GetElementsByTagName("a").Item(counter).OuterHtml.Contains("x=portal.PortalClassbook") Then
                    WebBrowser1.Document.Window.Frames.Item(0).Document.GetElementsByTagName("a").Item(counter).InvokeMember("click")
                End If
            Catch ex As NullReferenceException
                For x As Integer = 1 To 10
                    System.Threading.Thread.Sleep(100)
                    Application.DoEvents()
                Next
                If WebBrowser1.Document.Window.Frames.Item(0).Document.GetElementsByTagName("tr").Item(counter).OuterHtml.Contains("
                    GoTo loading2
            End Try
loading3:   Try
                WebBrowser1.Document.Window.Frames.Item(0).Document.GetElementById("schedule").InvokeMember("click")
            Catch ex As NullReferenceException
                For x As Integer = 1 To 10
                    System.Threading.Thread.Sleep(100)
                    Application.DoEvents()
                Next
                GoTo loading3
            End Try
        Next
    End Sub
End Class
